package top.lishou.learnplat.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lishou.learnplat.controller.exception.ValidCodeNotMatchException;
import top.lishou.learnplat.entity.Message;
import top.lishou.learnplat.entity.Senior;
import top.lishou.learnplat.service.ISeniorService;
import top.lishou.learnplat.utils.JsonResult;

import java.util.List;

/**
 * @Author: 20525
 * @Date: 2024/9/20 17:04
 */
@RestController
@RequestMapping("/senior")
public class SeniorController extends BaseController {

    @Autowired
    private ISeniorService seniorService;

    @GetMapping("/{id}")
    public JsonResult<Senior> getSeniorById(@PathVariable Integer id) {
        System.out.println("Requested ID: " + id);
        Senior senior = seniorService.getSeniorById(id);
        return new JsonResult<>(OK, senior);
    }

    @GetMapping("/search")
    public JsonResult<List<Senior>> searchSeniorsBySchool(String school) {
        List<Senior> seniors = seniorService.searchSeniorsBySchool(school);
        return new JsonResult<>(OK, seniors);
    }

    @PostMapping("/message/{id}")
    public JsonResult<String> addMessage(@PathVariable Integer id, String message) {
        Message msg = new Message();
        msg.setSenior(new Senior(id, null, null, null, null,null,null));
        msg.setMessage(message);
        boolean success = seniorService.addMessage(msg);
        if (success) {
            return new JsonResult<>(OK, "留言成功");
        } else {
            throw new ValidCodeNotMatchException("留言失败,请重试！");
        }
    }

    @GetMapping("/messages/{id}")
    public JsonResult<List<Message>> getMessagesBySeniorId(@PathVariable Integer id) {
        List<Message> message = seniorService.getMessagesBySeniorId(id);
        return new JsonResult<>(OK, message);
    }
}